package payroll.webapp

class DepartmentController extends AuthController {

    def add = {
		if (request.method == 'GET') {
			def department = new Department()
			[department:department]
		}
		else {
			def department = new Department(params['department'])
			
			if (department.save()) {
				redirect(action:'list')
			}
			else {
				render view:'add', model:[department:department]
			}
		}
	}
	
	def edit = {
		def department = Department.get(params.id)
		def map = [department: department]
		
		if(request.method == 'GET') {
			render view:'add', model:map
		}
		else
		{
			department.properties = params['department']
			if (department.save()) {
				redirect(action:'list')
			}
			else {
				render view:'add', model:map
			}
		}
	}
	
	def list = {
		[departments : Department.list()]
	}
	
	def delete = {
		def department = Department.get(params.id)
		
		if (department) {
			department.delete()
			redirect(action:'list')
		}
	}
}
